//
//  Ugc_Find_Tbv_WX.m
//  ZITHER
//
//  Created by weixin on 2019/1/14.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Ugc_Find_Tbv_WX.h"
#import "MainFirst_MagazineList_UgcCell_WX.h"
#import "MainFirst_MagazineList_PGCCell_WX.h"
#import "Ugc_Detail_VC_WX.h"
#import "MainFirst_Magazine_VC_WX.h"

@implementation Ugc_Find_Tbv_WX


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration];
        [self reload];
    }
    return self;
}

-(void)Starefreshing{
    [self reload];
}

-(void)reload{
    [[serviceDefualt loadRecommendcreationlistPage:0 reponseObjctClass:[CoverResultModelSub class]]subscribeNext:^(ResponseData * x) {
        if (x.success) {
            
            [self setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];
            
            CoverResultModelSub * model = [[CoverResultModelSub alloc]init];
            model = [CoverResultModelSub yy_modelWithDictionary:x.responseRawData];
            if (model.data.count == 0) {
                
                if (self.page == 0 ) {
                    [self setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                    [Tools showTips:@"暂无更多数据"];
                    [self endRefreshing];
                    [self.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.mj_footer endRefreshingWithNoMoreData];
                }
                return ;
            }else{
                if (self.page == 0) {
                    self.model = model;
                }else{
                    for (CoverCreateListModelSub *modelSub in model.data) {
                        [self.model.data addObject:modelSub];
                    }
                }
            }
            [self endRefreshing];
            [self reloadData];
        }else{
            [Tools showTips:x.errorInfo];
            [self endRefreshing];
        }
        
    }];
}

-(void)defaultConfiguration{
//    self.backgroundColor = [UIColor redColor];
    self.delegate = self;
    self.dataSource = self;
}

-(void)setModel:(CoverResultModelSub *)model{
    _model = model;
    [self reloadData];
}

#pragma mark -- tabdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.data.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CoverCreateListModelSub *model = self.model.data[indexPath.row];
    if ([model.relType isEqualToString:@"1"]) {
        return [self getMainFirst_MagazineList_UgcCell_WX:indexPath];
    }
    return [self getMainFirst_MagazineList_PGCCell_WX:indexPath];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CoverCreateListModelSub *modelSub = self.model.data[indexPath.row];
    if ([modelSub.relType isEqualToString:@"1"]) {
        Ugc_Detail_VC_WX *vc = [[Ugc_Detail_VC_WX alloc]init];
        vc.ugcId = modelSub.relId;
        vc.userID = modelSub.userId;
        [vc setBlockDel:^{
            [self.model.data removeObjectAtIndex:self.selectCellIndexPath];
            [self deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationFade];
        }];
        [self.getNav pushViewController:vc animated:YES];
    }else{
        MainFirst_Magazine_VC_WX *vc = [[MainFirst_Magazine_VC_WX alloc]init];
        vc.pgcId = modelSub.relId;
        [vc setBlockDel:^{
            [self.model.data removeObjectAtIndex:self.selectCellIndexPath];
            [self deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationFade];
        }];
        [self.getNav pushViewController:vc animated:YES];
    }
    self.selectCellIndexPath = indexPath.row;
    
}




-(MainFirst_MagazineList_PGCCell_WX*)getMainFirst_MagazineList_PGCCell_WX:(NSIndexPath*)indexPath{

    CoverCreateListModelSub *modelSub = self.model.data[indexPath.row];

    static NSString *sectionCell = @"MainFirst_MagazineList_PGCCell_WX";
    MainFirst_MagazineList_PGCCell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainFirst_MagazineList_PGCCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    cell.recommend = @"1";
    
    cell.model = modelSub;
    return cell;
}


-(MainFirst_MagazineList_UgcCell_WX*)getMainFirst_MagazineList_UgcCell_WX:(NSIndexPath*)indexPath{
    CoverCreateListModelSub *modelSub = self.model.data[indexPath.row];

    static NSString *sectionCell = @"MainFirst_MagazineList_UgcCell_WX";
    MainFirst_MagazineList_UgcCell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainFirst_MagazineList_UgcCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.recommend = @"1";
    
    cell.model = modelSub;
    return cell;
}

@end
